package com.sara.service.impl;

import com.sara.entity.*;
import com.sara.mapper.EmployeeRoleMapper;
import com.sara.mapper.PermissionMapper;
import com.sara.mapper.RoleMapper;
import com.sara.mapper.RolePermissionMapper;
import com.sara.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class permissionserviceImpl implements PermissionService {
    @Autowired   
    private EmployeeRoleMapper employeeRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    /**
     * 通过员工id查找角色
     *
     * @param id 员工id
     * @return 角色对象
     */
    @Override
    public List<Role> findRoleByEmployeeId(Integer id) {
        
        List<Role> roleList = null;
        if(StringUtils.isNumeric(Integer.toString(id))){
            EmployeeRoleExample employeeRoleExample = new EmployeeRoleExample();
            employeeRoleExample.createCriteria().andEmployeeIdEqualTo(id);
            List<EmployeeRole> employeeRoles = employeeRoleMapper.selectByExample(employeeRoleExample);
            for(EmployeeRole employeeRole : employeeRoles){
               Role role = roleMapper.selectByPrimaryKey(employeeRole.getRoleId());
               roleList.add(role);  
            }

             
        }
        return roleList;
        
        
    }

    /**
     * 查找所有权限
     *
     * @return 返回权限集合
     */
    @Override
    public List<Permission> findAllPermission() {
        return permissionMapper.selectByExample(new PermissionExample());
    }

    /**
     * 通过角色id查找权限集合
     *
     * @param id 角色id
     * @return 权限集合
     */
    @Override
    public List<Permission> findPermissionByRoleId(Integer id) {
        List<Permission> permissionList = null;
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andRoleIdEqualTo(id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(rolePermissionExample);
        
        for(RolePermission rolePermission : rolePermissions){
            Permission permission = permissionMapper.selectByPrimaryKey(rolePermission.getPermissionId());
            permissionList.add(permission);
        }
        return permissionList;
    }
}
